package com.ysd.lis.service.impl;

import cn.hutool.json.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ysd.common.Result;
import com.ysd.lis.entity.SysBasDictType;
import com.ysd.lis.mapper.sys.SysBasDictTypeMapper;
import com.ysd.lis.request.CommonPageDto;
import com.ysd.lis.service.SysBasDictTypeService;
import com.ysd.lis.util.ListToTreeUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * cold_chain.t_user 服务实现类
 * </p>
 *
 * @author peng
 * @since 2021-10-08
 */
@Service
@Slf4j
public class SysBasDictTypeServiceImpl extends ServiceImpl<SysBasDictTypeMapper, SysBasDictType> implements SysBasDictTypeService {
    @Autowired
    SysBasDictTypeMapper sysBasDictTypeMapper;
    @Override
    public Result findDictTypeTree(CommonPageDto pageParam) {
        LambdaQueryWrapper<SysBasDictType> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysBasDictType::getDelFlag,0);
        if(StringUtils.isNotBlank(pageParam.getOrgId())){
            queryWrapper.eq(SysBasDictType::getOrgId,pageParam.getOrgId());
        }
        queryWrapper.orderByAsc(SysBasDictType::getSx);
        List<SysBasDictType> dictTypeTree = sysBasDictTypeMapper.selectList(queryWrapper);
        JSONArray objects = ListToTreeUtil.forObjectToTree(dictTypeTree, "0", "id", "pid", "children");
        return Result.succ(1,"查询成功",objects);
    }

    @Override
    public Result addDictType(SysBasDictType sysBasDictType) {
        int insert = sysBasDictTypeMapper.insert(sysBasDictType);
        if(insert>0){
            return Result.succ(1, "添加成功", sysBasDictType);
        }else {
            return Result.succ(400, "添加失败", "");
        }
    }

    @Override
    public Result editDictType(SysBasDictType sysBasDictType) {
        int updateById = sysBasDictTypeMapper.updateById(sysBasDictType);
        if(updateById>0){
            return Result.succ(1, "修改成功", sysBasDictType);
        }else {
            return Result.succ(400, "修改失败", "");
        }
    }

    @Override
    public Result deletDictType(SysBasDictType sysBasDictType) {
        int deleteById = sysBasDictTypeMapper.deleteById(sysBasDictType.getId());
        if(deleteById>0){
            return Result.succ(1, "删除成功", sysBasDictType);
        }else {
            return Result.fail("删除失败");
        }
    }
}
